package csu.big_article.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import csu.big_article.mapper.ArticleMapper;
import csu.big_article.pojo.Article;
import csu.big_article.pojo.PageBean;
import csu.big_article.pojo.Result;
import csu.big_article.service.ArticleService;
import csu.big_article.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Override
    public void Publish(Article article) {
        Map<String,Object> map=ThreadLocalUtil.get();
        article.setCreateUser((Integer) map.get("id"));
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.Publish(article);
    }

    @Override
    public void Update(Article article) {
        Map<String,Object> map=ThreadLocalUtil.get();
        article.setCreateUser((Integer) map.get("id"));
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.Update(article);
    }

    @Override
    public void Delete(Integer id) {
        articleMapper.Delete(id);
    }

    @Override
    public PageBean<Article> GetList(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //1.创建pb对象
        PageBean<Article> pb =new PageBean<>();
        //2.开启分页查询PageHelper
        PageHelper.startPage(pageNum,pageSize);
        //
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer createUser=(Integer) map.get("id");
        List<Article> as = articleMapper.GetList(createUser,categoryId,state);
        Page<Article> page = (Page<Article>) as;

        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        //开始行号是(0,不包括查询的第一条的行号 结束行号3+3-1]是包括查询的最后一条的
//        System.out.println("总页数："+page.getPages());
//        System.out.println("总记录数"+page.getTotal());
//        System.out.println("开始行号"+page.getStartRow());
//        System.out.println("结束行号"+page.getEndRow());
//        System.out.println("当前页码"+page.getPageNum());
        return pb;
    }

    @Override
    public Article getDetailById(Integer id) {
        return articleMapper.getDetailById(id);
    }
}
